using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using GameFrameworkLibraly.BaseObjects;
using GameFrameworkLibraly.Controls;
using GameFrameworkLibraly.GlobalGameObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameFramework
{
    class LoadContentClass : LoadContentClassBase
    {
        private readonly ProgressBarControl _progressBarControl;
        readonly Dictionary<Textures, Texture2D> _textures = new Dictionary<Textures, Texture2D>();
        readonly Dictionary<Fonts, SpriteFont> _fonts = new Dictionary<Fonts, SpriteFont>();

        public LoadContentClass(MyGame game, ProgressBarControl progressBarControl) : base(game)
        {
            _progressBarControl = progressBarControl;
        }

        /// <summary>
        /// Takes textures from the dictionary. If there are no such texture than loads it from hard drive.
        /// </summary>
        /// <param name="name">Texture's name from enum</param>
        /// <returns>Texture</returns>
        public Texture2D GetTexture(Textures name)
        {
            if (!_textures.ContainsKey(name))
                _textures.Add(name, Game.Content.Load<Texture2D>("Textures/" + name));
            return _textures[name];
        }


        /// <summary>
        /// Takes font from the dictionary. If there are no such font than loads it from hard drive.
        /// </summary>
        /// <param name="name">Font's name from enum</param>
        /// <returns>Font</returns>
        public SpriteFont GetFont(Fonts name)
        {
            if (!_fonts.ContainsKey(name))
                _fonts.Add(name, Game.Content.Load<SpriteFont>("Fonts/" + name));
            return _fonts[name];
        }

        #region Overrides of LoadContentClassBase

        /// <summary>
        /// Load Content
        /// </summary>
        public override void Load()
        {
            #region Load Textures

            GetTexture(Textures.VolumeBarFull);
            Thread.Sleep(50);
            LoadingProgress = 3;
            GetTexture(Textures.VolumeBarPart);
            LoadingProgress = 6;
            Thread.Sleep(50);
            GetTexture(Textures.Manual1);
            LoadingProgress = 9;
            Thread.Sleep(50);
            GetTexture(Textures.Manual2);
            LoadingProgress = 12;
            Thread.Sleep(50);
            GetTexture(Textures.Manual3);
            LoadingProgress = 15;
            Thread.Sleep(50);
            GetTexture(Textures.Manual4);
            LoadingProgress = 18;
            Thread.Sleep(50);
            GetTexture(Textures.Manual5);
            LoadingProgress = 21;
            Thread.Sleep(50);
            GetTexture(Textures.Manual6);
            LoadingProgress = 24; Thread.Sleep(50);
            GetTexture(Textures.Manual7);
            LoadingProgress = 27; Thread.Sleep(50);
            GetTexture(Textures.Manual8);
            LoadingProgress = 30; Thread.Sleep(50);
            GetTexture(Textures.Manual9);
            LoadingProgress = 33; Thread.Sleep(50);
            GetTexture(Textures.Manual10);
            LoadingProgress = 36; Thread.Sleep(50);
            GetTexture(Textures.WaterTrail);
            LoadingProgress = 39; Thread.Sleep(50);
            GetTexture(Textures.Explosion);
            LoadingProgress = 42; Thread.Sleep(50);
            GetTexture(Textures.MagentaPixel);
            LoadingProgress = 45; Thread.Sleep(50);
            GetTexture(Textures.GreyPixel);
            LoadingProgress = 48; Thread.Sleep(50);
            GetTexture(Textures.Leaderboard);
            LoadingProgress = 51; Thread.Sleep(50);
            GetTexture(Textures.ExplosionFrame);
            LoadingProgress = 54; Thread.Sleep(50);
            GetTexture(Textures.Human);
            LoadingProgress = 57; Thread.Sleep(50);
            GetTexture(Textures.RopeBack);
            LoadingProgress = 60; Thread.Sleep(50);
            GetTexture(Textures.RopeFront);
            LoadingProgress = 63; Thread.Sleep(50);
            GetTexture(Textures.GameName);
            LoadingProgress = 66; Thread.Sleep(50);
            GetTexture(Textures.Logo);
            LoadingProgress = 69; Thread.Sleep(50);
            GetTexture(Textures.Chest);
            LoadingProgress = 72; Thread.Sleep(50);

            #endregion

            #region Load Fonts

            GetFont(Fonts.Segoe18);
            LoadingProgress = 75; 
            Thread.Sleep(50);
            GetFont(Fonts.Segoe42);
            LoadingProgress = 78; 
            Thread.Sleep(50);
            GetFont(Fonts.Segoe10);
            LoadingProgress = 81; 
            Thread.Sleep(50);
            GetFont(Fonts.Tahoma34);
            LoadingProgress = 95; 
            Thread.Sleep(200);
            GetFont(Fonts.Font1);
            LoadingProgress = 100; 
            Thread.Sleep(50);

            #endregion

            InvokeLoaded(EventArgs.Empty);
        }

        #endregion

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (LoadingProgress < 100)
            {
                _progressBarControl.Progress = LoadingProgress;
                _progressBarControl.Update(gameTime);
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
            if (LoadingProgress < 100)
                _progressBarControl.Draw(spriteBatch);
        }
    }


    /// <summary>
    /// Available fonts
    /// </summary>
    public enum Fonts
    {
        Segoe10,
        Segoe18,
        Segoe42,
        Tahoma34,
        Font1
    }


    /// <summary>
    /// Available textures
    /// </summary>
    public enum Textures
    {
        VolumeBarFull,
        VolumeBarPart,
        Manual1,
        Manual2,
        Manual3,
        Manual4,
        Manual5,
        Manual6,
        Manual7,
        Manual8,
        Manual9,
        Manual10,
        WaterTrail,
        Explosion,
        MagentaPixel,
        GreyPixel,
        Leaderboard,
        ExplosionFrame,
        Human,
        RopeBack,
        RopeFront,
        GameName,
        Logo,
        Chest
    }
}
